package com.shire.shire_interface.service.commandcenter.queues;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.utils.ChatUtil;
import com.shire.shire_interface.constants.WxConstants;
import com.shire.shire_interface.model.command.WxCommand;
import com.shire.shire_interface.model.timeline.WxTimelineTask;
import com.shire.shire_interface.model.user.WxAccount;
import com.shire.shire_interface.service.commandcenter.WxCommandService;
import com.shire.shire_interface.service.timeline.WxTimelineTaskService;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class TimelineCommandQueue extends GappedCommandQueue {

    @Resource
    private WxTimelineTaskService timelineTaskService;

    @Resource
    private WxCommandService commandService;


    private static TimelineCommandQueue instance;
    @PostConstruct
    public void init(){
        instance = this;
    }

    public static TimelineCommandQueue getInstance(){
        return instance;
    }

    public TimelineCommandQueue getQueueForAccount(WxAccount account){
        return new TimelineCommandQueue(account, WxConstants.CmdPriority.TIMLIMELINE, this.timelineTaskService,this.commandService);
    }

    public TimelineCommandQueue(){
        super();
    }

    public TimelineCommandQueue(WxAccount wxAccount, WxConstants.CmdPriority priority, WxTimelineTaskService timelineTaskService, WxCommandService commandService) {
        super(wxAccount, priority);
        this.timelineTaskService = timelineTaskService;
        this.commandService = commandService;
    }

    @Override
    public WxCommand getNextCommand() {
        synchronized (this.wxAccount.getWxid()) {
            if(this.lastReturnTime != null && ((new Date()).getTime() - this.lastReturnTime.getTime()) > 60* 1000){ //一分钟执行一次
                return null;
            }

            List<WxTimelineTask> timelineTaskList = timelineTaskService.getWxTimelineTaskToSend(wxAccount);


            WxTimelineTask task = null;
            for (WxTimelineTask tmp : timelineTaskList) {
                if (tmp.getSendtype() == 0) {
                    task = tmp;
                    break;
                } else {
                    if (tmp.getSendtime().getTime() <= (new Date()).getTime()) {
                        task = tmp;
                        break;
                    }
                }
            }

            if (task == null) return null; //no task to send

            JSONObject jsonObject = new JSONObject();

            WxConstants.CmdType cmdType = ChatUtil.getTimelineReqContent(task.getTimelinecontent(), jsonObject);

            curCmd = commandService.generateCmdObject(wxAccount.getCsid(), wxAccount.getId(), jsonObject.toJSONString(), WxConstants.CmdTaskType.TIMLIMELINE, task.getId(), cmdType, WxConstants.CmdPriority.TIMLIMELINE);

            task.setCmdid(curCmd.getId().intValue());
            task.setCmdstatus(curCmd.getRststatus());
            task.setStatus(1);
            task.setUpdatetime(new Date());
            timelineTaskService.updateWxTimelineTask(task);

            lastReturnTime = new Date();
        }

        return super.getNextCommand();
    }
}
